[Bash] problem with script in sed ...
I made a little script that searches and substitutes parameters in a
configuration file but I've a problem:
Configuration file conf.tmp:
------------------------------------------------------------ -----------
parametro1=mario
parametro2=ugo
parametro3=elena
parametro4=sara
------------------------------------------------------------ -----------
Script:
------------------------------------------------------------ -----------
#!/bin/bash -x
TMP_FILE=./test.tmp
CONF_FILE=./conf.tmp
TOT_PARAMETERS=$#;
scratch=""
if test $[TOT_PARAMETERS%2] -ne 0
then
echo "Usage: $0 desc"
exit 1
fi
if test $# -lt 2
then
echo "Usage: $0 desc"
exit 1
fi
ctr=0
until [ -z "$1" ]
do
PARAMETER=$1
NEW_VALUE=$2
if test ${ctr} -eq 0
then
scratch="sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
else
scratch="${scratch} | sed
\"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
fi
let ctr=ctr+1
shift 2
done
echo -e ""
echo "COMMAND LINE: cat ${CONF_FILE} | ${scratch} > $TMP_FILE";
echo -e ""
cat ${CONF_FILE} | ${scratch} > $TMP_FILE
# ... continua
------------------------------------------------------------ -----------
executing the script:
------------------------------------------------------------ -----------
:~$ ./script.sh parametro1 pippo parametro2 pluto parametro3 paperino
------------------------------------------------------------ -----------
look the output, the printed command line is correct but it works only
if executed out of the script.
The same command in the script fails.
Command line:
------------------------------------------------------------ -----------
cat ./conf.tmp | sed "s%^parametro1=.*%parametro1=pippo%g" | sed
"s%^parametro2=.*%parametro2=pluto%g" | sed
"s%^parametro3=.*%parametro3=paperino%g" > ./test.tmp
------------------------------------------------------------ -----------
in debug mode I look that:
------------------------------------------------------------ -----------
sed '"s%^parametro1=.*%parametro1=pippo%g"' '|' sed
'"s%^parametro2=.*%parametro2=pluto%g"' '|' sed
'"s%^parametro3=.*%parametro3=paperino%g"'
------------------------------------------------------------ -----------
sed: -e expression #1, char 1: Unknown command: `"'
Any hints?
Re: [Bash] problem with script in sed ...
On Thu, 21 Jul 2005 16:05:43 -0700, icui wrote:
> I made a little script that searches and substitutes parameters in a
> configuration file but I've a problem:
>
>
> Configuration file conf.tmp:
> ------------------------------------------------------------ -----------
> parametro1=mario
> parametro2=ugo
> parametro3=elena
> parametro4=sara
> ------------------------------------------------------------ -----------
>
> Script:
>
> ------------------------------------------------------------ -----------
> #!/bin/bash -x
>
> TMP_FILE=./test.tmp
> CONF_FILE=./conf.tmp
> TOT_PARAMETERS=$#;
> scratch=""
>
> if test $[TOT_PARAMETERS%2] -ne 0
> then
> echo "Usage: $0 desc"
> exit 1
> fi
>
> if test $# -lt 2
> then
> echo "Usage: $0 desc"
> exit 1
> fi
>
> ctr=0
> until [ -z "$1" ]
> do
> PARAMETER=$1
> NEW_VALUE=$2
> if test ${ctr} -eq 0
> then
> scratch="sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
> else
> scratch="${scratch} | sed
> \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
> fi
> let ctr=ctr+1
> shift 2
> done
>
> echo -e ""
> echo "COMMAND LINE: cat ${CONF_FILE} | ${scratch} > $TMP_FILE";
> echo -e ""
>
> cat ${CONF_FILE} | ${scratch} > $TMP_FILE
>
> # ... continua
>
> ------------------------------------------------------------ -----------
>
>
> executing the script:
> ------------------------------------------------------------ -----------
> :~$ ./script.sh parametro1 pippo parametro2 pluto parametro3 paperino
> ------------------------------------------------------------ -----------
>
>
> look the output, the printed command line is correct but it works only
> if executed out of the script.
> The same command in the script fails.
>
> Command line:
> ------------------------------------------------------------ -----------
> cat ./conf.tmp | sed "s%^parametro1=.*%parametro1=pippo%g" | sed
> "s%^parametro2=.*%parametro2=pluto%g" | sed
> "s%^parametro3=.*%parametro3=paperino%g" > ./test.tmp
> ------------------------------------------------------------ -----------
>
> in debug mode I look that:
> ------------------------------------------------------------ -----------
> sed '"s%^parametro1=.*%parametro1=pippo%g"' '|' sed
> '"s%^parametro2=.*%parametro2=pluto%g"' '|' sed
> '"s%^parametro3=.*%parametro3=paperino%g"'
> ------------------------------------------------------------ -----------
>
> sed: -e expression #1, char 1: Unknown command: `"'
You want to 'eval' $script.
It would be more efficient to build up a single 'sed' program, rather than
build up a pipeline of sed commands.
#!/bin/bash
case $# in
0|*[13579]) echo $0: Usage p1 v1 p2 v2 ... >&2 ; exit 1;;
esac
cmd=""
while [ -n "$1" ]
do
cmd="${cmd}s%^$(printf "%q" $1).*%${1}=${2}%;"
shift 2
done
sed -e "$cmd" conf.file > test.tmp
Re: [Bash] problem with script in sed ...
icui wrote:
> I made a little script that searches and substitutes parameters in a
> configuration file but I've a problem:
>
>
> Configuration file conf.tmp:
> ------------------------------------------------------------ -----------
> parametro1=mario
> parametro2=ugo
> parametro3=elena
> parametro4=sara
> ------------------------------------------------------------ -----------
>
> Script:
>
> ------------------------------------------------------------ -----------
> #!/bin/bash -x
>
> TMP_FILE=./test.tmp
> CONF_FILE=./conf.tmp
> TOT_PARAMETERS=$#;
> scratch=""
>
> if test $[TOT_PARAMETERS%2] -ne 0
> then
> echo "Usage: $0 desc"
> exit 1
> fi
>
> if test $# -lt 2
> then
> echo "Usage: $0 desc"
> exit 1
> fi
>
> ctr=0
> until [ -z "$1" ]
> do
> PARAMETER=$1
> NEW_VALUE=$2
> if test ${ctr} -eq 0
> then
> scratch="sed \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
> else
> scratch="${scratch} | sed
> \"s%^$PARAMETER=.*%${PARAMETER}=${NEW_VALUE}%g\" "
> fi
> let ctr=ctr+1
> shift 2
> done
>
> echo -e ""
> echo "COMMAND LINE: cat ${CONF_FILE} | ${scratch} > $TMP_FILE";
> echo -e ""
>
> cat ${CONF_FILE} | ${scratch} > $TMP_FILE
>
> # ... continua
>
> ------------------------------------------------------------ -----------
>
>
> executing the script:
> ------------------------------------------------------------ -----------
> :~$ ./script.sh parametro1 pippo parametro2 pluto parametro3 paperino
> ------------------------------------------------------------ -----------
>
>
> look the output, the printed command line is correct but it works only
> if executed out of the script.
> The same command in the script fails.
>
> Command line:
> ------------------------------------------------------------ -----------
> cat ./conf.tmp | sed "s%^parametro1=.*%parametro1=pippo%g" | sed
> "s%^parametro2=.*%parametro2=pluto%g" | sed
> "s%^parametro3=.*%parametro3=paperino%g" > ./test.tmp
> ------------------------------------------------------------ -----------
>
> in debug mode I look that:
> ------------------------------------------------------------ -----------
> sed '"s%^parametro1=.*%parametro1=pippo%g"' '|' sed
> '"s%^parametro2=.*%parametro2=pluto%g"' '|' sed
> '"s%^parametro3=.*%parametro3=paperino%g"'
> ------------------------------------------------------------ -----------
>
> sed: -e expression #1, char 1: Unknown command: `"'
>
> Any hints?
>
You need escapes and eval more accurate quoting. Rather than peeling the
onion on that script, you could just replace eveyrhing from "ctr=0" on
with this:
gawk -v a="$*" ' BEGIN{ FS=OFS="="
c = split(a,b," ")
for (i=1; i<=c; i+=2) args[b[i]]=b[i+1]
}
$1 in args {$2 = args[$1]} 1' conf.tmp
Whether you check your argument counts inside or outside the awk script
is up for grabs.
Regards,
Ed.
Re: problem with script in sed ...
Thanks for your precious help